import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'test/Item.dart';

class RefreshIndex extends StatefulWidget {
  RefreshIndex({super.key});

  @override
  State<StatefulWidget> createState() {
    return RefreshIndexState();
  }
}

class RefreshIndexState extends State<RefreshIndex> {
  final RefreshController _refreshController = RefreshController();

  List<String> data = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];

  @override
  Widget build(BuildContext context) {


    print("------>>>>>>>>>>>>> ${RefreshLocalizations.of(context)?.currentLocalization}");
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Refresh Index',
          style: TextStyle(fontSize: 20, color: Colors.blue),
        ),
      ),
      body: SmartRefresher(
        enablePullUp: true,
        enablePullDown: true,
        header: const MaterialClassicHeader(),
        footer: const ClassicFooter(
          loadStyle: LoadStyle.showWhenLoading,
        ),
        onRefresh: () async {
          await Future.delayed(const Duration(seconds: 2));
          setState(() {
            data.clear();
          });
          for (int i = 0; i < 10; i++) {
            data.add("-- index $i");
          }
          setState(() {});
          _refreshController.refreshCompleted();
        },
        onLoading: () async {
          await Future.delayed(const Duration(seconds: 2));
          for (int i = 0; i < 10; i++) {
            data.add("1");
          }

          setState(() {});
          _refreshController.loadComplete();
        },
        controller: _refreshController,
        child: buildCtn(),
      ),
    );
  }

  Widget buildCtn() {
    return ListView.separated(
      physics: const ClampingScrollPhysics(),
      padding: const EdgeInsets.only(left: 5, right: 5),
      itemBuilder: (c, i) => Item(
        title: data[i],
      ),
      separatorBuilder: (context, index) {
        return Container(
          height: 0.5,
          color: Colors.greenAccent,
        );
      },
      itemCount: data.length,
    );
  }

  Widget footer() {
    return CustomFooter(
      builder: (context, mode) {
        Widget body;
        if (mode == LoadStatus.idle) {
          body = const Text("上拉加载");
        } else if (mode == LoadStatus.loading) {
          body = const CupertinoActivityIndicator();
        } else if (mode == LoadStatus.failed) {
          body = const Text("加载失败！点击重试！");
        } else if (mode == LoadStatus.canLoading) {
          body = const Text("松手,加载更多!");
        } else {
          body = const Text("没有更多数据了!");
        }
        return Container(
          height: 55.0,
          child: Center(child: body),
        );
      },
    );
  }
}
